/**
 * Created by lwy on 2017/5/21.
 */
var page2 = 0;
var num=0;
//做单列表
$(function(){
    doOrder();
})


function doOrder(){
    page2++;
    $.ajax({
        url:'/zuoDanC/userOrderList.json',
        dataType:'json',
        type:'post',
        data:{
            token:getItem('token'),
            page:page2,
        },
        beforeSend:function(){
          if(page2==1){
              $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('#order'));
          }
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                var data = data.datas;
                if(page2==1){
                    $('#order').empty();
                    if(data.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../img/kong.png" alt="暂无数据"></div>').appendTo($('#order'));
                    }
                }
                for(var i=0;i<data.length;i++){
                    $('<div class="order-list">'+
                        '<div class="mui-clearfix">'+
                        '<div class="order-list-left"><span class="line"></span><b></b><span class="line line2"></span></div>'+
                        '<div class="order-list-sanjiao"></div>'+
                        '<div class="order-list-right">'+
                        '<span class="time">'+data[i].createTime+'</span><img src="../img/order-more.png"/><a class="detail"  name="'+data[i].shopName+'" code="'+data[i].code+'" goodsName="'+data[i].goodsName+'" goodsNum="'+data[i].goodsNum+'" goodsPrice="'+data[i].goodOnePrice+'" total="'+data[i].shopYingYeMoney+'" payMoney="'+data[i].shopPayMoney+'" integral="'+data[i].jifen+'">查看详情</a>'+
                        '<p class="line3"></p>'+
                        '<p class="two-line"><i class="money-sign">¥</i><span class="money">'+data[i].shopYingYeMoney+'</span><span class="state">'+data[i].shopState+'</span><img src="../img/order-state.png" alt="订单状态"></p>'+
                        '<p class="code">'+data[i].code+'</p>'+
                        '</div></div></div>').appendTo($('#order'))
                }
                num=data.length;
            }
        }
    })
    return num;
}

mui('body').on('tap','.detail',function(){
    var code=$(this).attr('code');
    var name=$(this).attr('name');
    var goodsName=$(this).attr('goodsName');
    var goodsNum=$(this).attr('goodsNum');
    var goodsPrice=$(this).attr('goodsPrice');
    var total=$(this).attr('total');
    var payMoney=$(this).attr('payMoney');
    var integral=$(this).attr('integral');
    window.location.href=encodeURI(encodeURI("order-detail.html?code="+code+"&name="+name+"&goodsName="+goodsName+"&goodsNum="+goodsNum+"&goodsPrice="+goodsPrice+"&total="+total+"&payMoney="+payMoney+"&integral="+integral));
})

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doOrder();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});

$('#online').click(function () {
    window.location.href="userOrder_v2.html"
})